'''
This file contains all the available modules for ThinkV
'''
import wx, utils

class BaseModule(object):
    
    # provides the possible controllers to be used by the MainPage class
    hooks = ()
    
    # holds the default configuration for the module
    defaultConfig = {}
    
    def __init__(self, parent):
        self.parent = parent
    
    def getController(self):
        '''Create the layout and set up the controllers
        
        This method should be overwritten in child classes
        '''
        
    def isAvailable(self):
        '''Check if the given module has all the requirements.'''
        return False

class Touchpad(BaseModule):
    '''
    Toggle touchpad on/off
    '''
    
    hooks = ('getController',)
    
    defaultConfig = {
        '/Thouchpad/on' : True,                 
    }
    
    def getController(self):
        self.toggle = wx.CheckBox(self.parent, wx.NewId(), 'Swith Touchpad on')
        self.toggle.SetValue(self.isOn())
        wx.EVT_CHECKBOX(self.parent, self.toggle.GetId(), self.toggleCheckBox)
        print 'Controller initialized %s' % self.__class__

    def isOn(self):
        '''
        Returns True if the touchpad is on
        '''
        for line in utils.execute('synclient -l', split_lines=True):
            if line.split('=')[0].strip()=='TouchpadOff':
                return not int(line.split('=')[1].strip())
        return self.parent.errorMessage('There is something strange with the Touchpad')

    def toggleCheckBox(self, event):
        if event.Checked():
            utils.execute('gksudo -D "Switch touchpad on" synclient TouchpadOff=0')
            self.parent.cfg.WriteBool('Touchpad/on', True)
        else:
            utils.execute('gksudo -D "Switch touchpad off" synclient TouchpadOff=1')
            self.parent.cfg.WriteBool('Touchpad/on', False)
        #self.parent.OnExit(None)
        self.isOn()
    
class PowerSave(BaseModule):
    '''
    Allow for different powersaving schemes and settings
    '''
    hooks = ('getBatteyController',)
    
    def getController(self):
        self.getBatteryController()
        self.getUsageController()
        
    def getBatteryController(self):
        '''Controls for start/stop recharing of the battery'''
        pass
    
    def getUsageController(self):
        '''Controls the running of selected services to save battery power'''
        pass
    
class Applications(BaseModule):
    '''
    Provide a shortcut for registered applications. e.g: run Firefox
    '''
    
    def addApplication(self):
        '''Add a new application to the list'''
        pass
    
    def saveApplication(self, appname, path, shortcut=False):
        '''Save the added application'''
        self.parent.cfg.Write('/Applications/%s' % appname, path)
        if shortcut:
            self.parent.cfg.Write('/Applications/Shourtcuts/%s' % appname, path)
    
    def deleteApplication(self):
        pass
    
    def editApplication(self):
        pass
    
class MainPage(BaseModule):
    '''
    As the modules are on separate pages, one can collect 
    what he would like to see on the main page
    '''
    
    def getController(self):
        # read all the other modules hooks variable as it contains the possible controllers
        pass
    